package cn.itcast.bos.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.repository.CourierRepository;
import cn.itcast.bos.repository.FixedAreaRepository;
import cn.itcast.bos.repository.TakeTimeRepository;
import cn.itcast.bos.service.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	@Autowired
	private CourierRepository courierRepository;

	@Override
	public void save(FixedArea fixedArea) {
		fixedAreaRepository.save(fixedArea);
	}

	@Override
	public Page<FixedArea> findPageDataFixedArea(Specification<FixedArea> specification, Pageable pageable) {
		return fixedAreaRepository.findAll(specification, pageable);
	}

	@Override
	public void associationCourierToFixedArea(FixedArea fixedArea, String courierId, String takeTimeId) {
		
		
		FixedArea persistFixedArea  = fixedAreaRepository.findOne(fixedArea.getId());
		TakeTime takeTime = takeTimeRepository.findOne(Integer.parseInt(takeTimeId));
		Courier courier = courierRepository.findOne(Integer.parseInt(courierId));
		//快递员关联到定区,(多对多数据库,由定去一端维护关联关系)
		persistFixedArea.getCouriers().add(courier);
		courier.setTakeTime(takeTime);
		
	}

}
